package com.bjsxt.service;

import com.bjsxt.source.MessageSource;
import org.apache.rocketmq.common.message.MessageConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

@Service
public class MessageSendService {
    private static Logger logger = LoggerFactory.getLogger(MessageSendService.class) ;

    @Autowired
    private MessageSource messageSource ;

    // 1 普通字符串消息

    public void send(String msg){
        Message<String> message = MessageBuilder.withPayload(msg).build();
        messageSource.output1().send(message) ;
    }
    // 2 带tag的消息

    public void send(String msg ,String tag){
        Message<String> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_TAGS, tag)
                .build();
        messageSource.output1().send(message) ;
    }
    // 3 发送对象消息

    public <T> void sendObject(T msg ,String tag){
        Message<T> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_TAGS, tag)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        messageSource.output1().send(message) ;
    }

    // 4 发送事务消息（half）
     public <T> void sendTransactionMsg(T t ,int num){
         Message<T> message = MessageBuilder.withPayload(t)
                 .setHeader("type", String.valueOf(num))
                 .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                 .build();
         messageSource.output2().send(message) ;
     }

    // 5 发送消息到pull 的目的地址，为了演示我们消息的手动拉取
    public void sendMassiveMsg(String msg){
        messageSource.output3().send(MessageBuilder.withPayload(msg).build()) ;
    }
}
